﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SpringStudio.Common;
using Sdepci.InfoCode.BLL.Authority;

namespace Sdepci.InfoCode.Web.Authority
{
    public partial class UserRole : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.ViewState["Template"] = this.Template.InnerHtml;
                this.BindData();
            }
        }

        private void BindData()
        {
            string html = string.Empty;
            if (this.Request.QueryString["UserId"] != null && this.Request.QueryString["UserId"].Trim().Length > 0)
            {
                User user = new User(this.Request.QueryString["UserId"].Trim());
                this.lblUserInfo.Text = "用户证档号：" + user.UserId + "　姓名：" + user.UserName;

                List<Role> list = new RoleManager().GetRoleList();
                foreach (Role role in list)
                {
                    string template = this.ViewState["Template"].ToString();
                    template = template.Replace("{RoleGuid}", role.RoleGuid);
                    template = template.Replace("{RoleName}", role.RoleName);

                    if (role.RoleGuid == user.RoleGuid)
                    {
                        template = template.Replace("{checked}", "checked=\"checked\"");
                    }
                    else
                    {
                        template = template.Replace("{checked}", "");
                    }

                    html += template;
                }
            }

            this.Template.InnerHtml = html;
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            bool result = false;
            if (this.Request.QueryString["UserId"] != null && this.Request.QueryString["UserId"].Trim().Length > 0)
            {
                if (this.Request.Form["RoleGuid"] != null && this.Request.Form["RoleGuid"].Trim().Length > 0)
                {
                    result = new UserManager().ModifyRole(this.Request.QueryString["UserId"].Trim(), this.Request.Form["RoleGuid"].Trim());
                }
            }

            string msg = result ? "角色设置成功！" : "角色设置失败！";
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "\");self.returnValue=true;self.close();</script>");
        }
    }
}
